/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TableEditPanel.java
 *
 * Created on Apr 7, 2010, 2:01:51 PM
 */

package edu.asu.cse360.client.ui.frame;

import edu.asu.cse360.client.context.RestaurantContext;
import edu.asu.cse360.context.ServiceFactory;
import edu.asu.cse360.model.Table;
import edu.asu.cse360.model.TableStatus;
import edu.asu.cse360.service.TableService;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;

/**
 *This allows the staff to edit the status of the table
 * @author ggreiner
 */
public class TableEditPanel extends javax.swing.JPanel {

    private TableTableModel tableModel;
    private TableService tableService;
    private Table selectedTable;
    private ComboBoxModel statusModel;

    /** Creates new form TableEditPanel */
    public TableEditPanel() {
        statusModel = new DefaultComboBoxModel(TableStatus.values());
        tableService = ServiceFactory.getTableService();
        tableModel = new TableTableModel();
        refreshTableModel();
        initComponents();
        tableTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {
                handleListSelection(e.getValueIsAdjusting());
            }


        });
        if(!tableModel.getTables().isEmpty()){
            selectedTable = tableModel.getTableAt(0);
            tableTable.setRowSelectionInterval(0,0);
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tableTable = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        updateStatusBtn = new javax.swing.JButton();
        statusComboBox = new javax.swing.JComboBox();

        tableTable.setModel(tableModel);
        jScrollPane2.setViewportView(tableTable);

        jButton1.setText("Refresh");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(30, 30, 30)
                .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 109, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(367, Short.MAX_VALUE))
            .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(32, Short.MAX_VALUE)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jButton1)
                .addContainerGap(406, Short.MAX_VALUE))
            .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(jPanel1Layout.createSequentialGroup()
                    .add(51, 51, 51)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 348, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(53, Short.MAX_VALUE)))
        );

        updateStatusBtn.setText("Update Status");
        updateStatusBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateStatusBtnActionPerformed(evt);
            }
        });

        statusComboBox.setModel(statusModel);
        statusComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                statusComboBoxActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(132, 132, 132)
                        .add(updateStatusBtn))
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(94, 94, 94)
                        .add(statusComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 205, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(24, 24, 24)
                .add(statusComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(88, 88, 88)
                .add(updateStatusBtn)
                .addContainerGap(96, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(48, 48, 48)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap(20, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(39, 39, 39))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void statusComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_statusComboBoxActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_statusComboBoxActionPerformed

    private void updateStatusBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateStatusBtnActionPerformed
        TableStatus selectedStatus = (TableStatus) statusComboBox.getSelectedItem();
        if(selectedTable != null){
            selectedTable.setStatus(selectedStatus);
            tableService.save(selectedTable);
            int selectedIndex = tableModel.indexOf(selectedTable);
            tableModel.fireTableCellUpdated(selectedIndex, 1);
        }
    }//GEN-LAST:event_updateStatusBtnActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        int selectedRow = tableTable.getSelectedRow();
        this.refreshTableModel();
        if(selectedRow != -1){
            tableTable.setRowSelectionInterval(selectedRow,selectedRow);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void handleListSelection(boolean isAdjusting) {
        if(isAdjusting)return;
        int selectedRow = tableTable.getSelectedRow();
        if(selectedRow == -1){
            selectedTable = null;
        }else{
            selectedTable = tableModel.getTableAt(selectedRow);
        }
        updateTableFormFields();
    }
    private void refreshTableModel() {
        List<Table> tables = tableService.getTables(RestaurantContext.getRestaurant());
        tableModel.setTables(tables);
    }

    private void updateTableFormFields() {
        if(selectedTable == null){
            statusComboBox.setSelectedItem(TableStatus.DEFAULT);
        }else{
            statusComboBox.setSelectedItem(selectedTable.getStatus());
        }
    }

    private class TableTableModel extends AbstractTableModel{
        private String[] columns = new String[]{"Table", "Status"};
        private List<Table> tables;

        public TableTableModel(){
            this(null);
        }

        public TableTableModel(List<Table> tables){
            this.tables = new ArrayList<Table>();
            if(tables != null){
                tables.addAll(tables);
            }
        }


        public int getRowCount() {
            return tables.size();
        }

        public int getColumnCount() {
            return columns.length;
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            Object value = null;
            Table table = tables.get(rowIndex);
            switch(columnIndex){
                case 0:
                    value = table.getTableNumber();
                    break;
                case 1:
                    value = table.getStatus();
                    break;
            }
            return value;
        }

        private void setTables(List<Table> tables) {
            if(tables == null){
                this.tables.clear();
            }else{
                this.tables = tables;
            }
            tableModel.fireTableDataChanged();
        }
        
        @Override
        public String getColumnName(int column) {
            return columns[column];
        }

        private List<Table> getTables() {
            return tables;
        }

        private Table getTableAt(int i) {
            return tables.get(i);
        }

        public int indexOf(Table table){
            return tables.indexOf(table);
        }
        
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JComboBox statusComboBox;
    private javax.swing.JTable tableTable;
    private javax.swing.JButton updateStatusBtn;
    // End of variables declaration//GEN-END:variables

}
